package com.byzk.p2p.admin.system.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.DateUtils;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.system.dto.GoodBuy;
import com.byzk.p2p.admin.system.service.GoodBuyService;
import com.byzk.p2p.admin.system.vo.GoodBuyVo;

@Controller
@RequestMapping("/admin/goodBuy")
public class GoodBuyController extends AdminBaseController {

	@Autowired
	private GoodBuyService goodBuyService;

	@RequestMapping("/list")
	public String listGoodBuy(Model model, 
			 		@RequestParam(value="index",required=false,defaultValue="1") int index,
			 		@RequestParam(value="limit",required=false,defaultValue="10") int limit,
			 		@RequestParam(value="startTime",required=false)String startTime,
			 		@RequestParam(value="endTime",required=false)String endTime) throws Exception {
		GoodBuyVo info = new GoodBuyVo();
		info.setIndex(index);
		info.setLimit(limit);
		if (StringUtils.isNotBlank(startTime)) {
			info.setStartTime(DateUtils.getRealDate(DateUtils.convert(startTime, DateUtils.YEAR_MOUTH_DAY_FORMAT)));
			model.addAttribute("startTime", startTime);
		}
		if (StringUtils.isNotBlank(endTime)) {
			info.setEndTime(DateUtils.getRealDate(DateUtils.convert(endTime, DateUtils.YEAR_MOUTH_DAY_FORMAT)));
			model.addAttribute("endTime", endTime);
		}
		PageInfo<GoodBuyVo> pageInfo = goodBuyService.listGoodBuy(info);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		return ftl(Thread.currentThread().getStackTrace());
	}

	/**
	 * 新增或修改
	 * 
	 * @param good
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/saveOrUpdate")
	public void saveOrUpdate(Model model, @RequestBody GoodBuy info) throws Exception {
		try {
			goodBuyService.saveOrUpdate(info);
			super.printJSON(new JsonMsg(-1, "新增成功"));
		} catch (Exception e) {
			// TODO: handle exception
			super.printJSON(new JsonMsg(1, "新增失败"));
		}
	}
	
	/**
	 * 删除商品类型
	 * @param model
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public void deleteGood(Model model, @RequestParam("id") Integer id) throws Exception {
		try {
			goodBuyService.delete(id);
			super.printJSON(new JsonMsg(-1, "删除成功"));
		} catch (Exception e) {
			// TODO: handle exception
			super.printJSON(new JsonMsg(1, "删除失败"));
		}
	}
	
	/**
	 * 商品发货
	 * @param model
	 * @param info
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/sendGood",method=RequestMethod.POST)
	public void sendGood(Model model, @ModelAttribute GoodBuy info) throws Exception{
		info.setStatus(1);
		int i = goodBuyService.saveOrUpdate(info);
		if(i>0){
			printJSON(new JsonMsg(-1, "操作成功！"));
			return;
		}
		printJSON(new JsonMsg(-1, "操作失败！"));
	}
	
}
